#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
聊天记录证据分析API服务启动脚本

使用方法:
1. 确保已安装依赖: pip install -r requirements.txt
2. 设置环境变量: set ARK_API_KEY=your_api_key
3. 运行此脚本: python start_server.py
"""

import os
import sys
import uvicorn
from evidence_analysis_api import app

def check_environment():
    """检查环境配置"""
    print("检查环境配置...")
    
    # 检查API Key
    api_key = os.environ.get("ARK_API_KEY")
    if not api_key:
        print("❌ 错误: 未设置ARK_API_KEY环境变量")
        print("请设置环境变量: set ARK_API_KEY=your_api_key")
        return False
    else:
        print(f"✅ ARK_API_KEY已设置 (长度: {len(api_key)})")
    
    # 检查依赖包
    try:
        import fastapi
        import openai
        print(f"✅ FastAPI版本: {fastapi.__version__}")
        print(f"✅ OpenAI版本: {openai.__version__}")
    except ImportError as e:
        print(f"❌ 错误: 缺少依赖包 {e}")
        print("请安装依赖: pip install -r requirements.txt")
        return False
    
    return True

def main():
    print("=== 聊天记录证据分析API服务 ===")
    print("版本: 1.0.0")
    print("作者: Assistant")
    print()
    
    # 检查环境
    if not check_environment():
        sys.exit(1)
    
    print("\n🚀 启动API服务...")
    print("服务地址: http://localhost:8080")
    print("API文档: http://localhost:8080/docs")
    print("健康检查: http://localhost:8080/health")
    print()
    print("按 Ctrl+C 停止服务")
    print("=" * 50)
    
    try:
        # 启动服务
        uvicorn.run(
            app,
            host="0.0.0.0",
            port=8080,
            reload=False,  # 生产环境建议设为False
            access_log=True,
            log_level="info"
        )
    except KeyboardInterrupt:
        print("\n👋 服务已停止")
    except Exception as e:
        print(f"\n❌ 服务启动失败: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()